<template>
    <div>

        <h4>课程</h4>
        <br>
        <table border="1">
            <tr>
                <td>序号</td>
                <td>课程名</td>
                <td>内容</td>
                <td>操作</td>
            </tr>
            <tr v-for="(course,index) in courselist">
                <td>{{index+1}}</td>
                <td>{{course.title}}</td>
                <td>{{course.content}}</td>
                <td><button @click="select(course.id)">选课</button></td>
            </tr>
        </table>
        <hr>


        <h4>学生</h4>
        <br>
        <table border="1">

            <tr>
                <td>序号</td>
                <td>名</td>
            </tr>

            <tr v-for="(student,index) in studentlist">
                <td>{{index+1}}</td>
                <td>{{student.username}}</td>
            </tr>

        </table>

    </div>
</template>

<script>
export default {
    data:function(){
        return{
            courselist:[],
            studentlist:[],
            // uid:""
        }
    },

    methods:{
        init:function(){

            this.axios({
                url:"http://127.0.0.1:8000/courselist/",
                method:'get'
            }).then((res)=>{
                if(res.data.code==200){
                    this.courselist=res.data.message
                }else{
                    alert(res.data.code)
                }
            }),

            this.axios({
                url:"http://127.0.0.1:8000/studentlist/",
                method:'get'
            }).then((res)=>{
                if(res.data.code==200){
                    this.studentlist=res.data.message
                }else{
                    alert(res.data.code)
                }
            })
        },
        select:function(cid){
            var uid = JSON.parse(sessionStorage.getItem('uid'))
            this.uid = uid
            let formData = new FormData();
            formData.append("uid",uid);
            formData.append("cid",cid);

            this.axios({
                url:"http://127.0.0.1:8000/index/",
                method:'post',
                data:formData,
            }).then((res)=>{
                if(res.data.code==200){
                    alert(res.data.message)
                }else{
                    alert(res.data.message)
                }
            })
        }
    },
    mounted:function(){
        this.init()
    }
}
</script>
